<?php
/**
 * Created by PhpStorm.
 * User: user1
 * Date: 16-10-26
 * Time: 下午2:55
 */

namespace Admin\Controller;
use Admin\Model\Page;
use Think\Exception;


class NoticeController extends BaseController
{
    public function __construct($baseModel)
    {
        parent::__construct(D('Notice'));
        $this->bcItemPush('公告管理', U('Notice/index'));
    }

    private static $_validate = array(
        array('title', 'require', '公告标题不能为空！', 1, 'regex', 3),
        array('content', 'require', '公告内容不能为空！', 1, 'regex', 3),
    );

    public function index()
    {
        $model = D('Notice');
        //构建查询条件
        $pagesize = 10;
        $title = I('title', '', 'htmlspecialchars');
        $pagenumber = I('pagenum', '');
        $map = [];
        if (empty($pagenumber) || $pagenumber <= 0)
            $pagenumber = 1;
        if ($title != '') {
            $map['t_notice.title'] = array('like', '%' . $title . '%');
        }
        $list = $model->getByConditionPage($map, $pagenumber, $pagesize);
        foreach ($list as $key=>$value){
            $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
            $url = "$protocol$_SERVER[HTTP_HOST]/Home/Notice/detail?notice_id=".$value['id'];
            $list[$key]['url'] = $url;
        }
        $recordcount = $model->where($map)->count();
        //封装分页对象
        $page = new Page($pagenumber, $recordcount, $pagesize);
        $page = (array)$page;//转换为数组
//        var_dump($list);
        //显示数据
        $this->assign('page', $page);
        $this->assign('title', $title);
        $this->assign('pagenumber', $pagenumber);
        $this->assign('list', $list);
        $this->assign('recordcount', $recordcount);
        $this->display('index');
    }

    public function addUI()
    {
        $this->bcItemPush('添加公告', U('Notice/addUI'));
        $this->display('save');
    }

    protected function doAddBefore($data)
    {
        $this->checkName($data, '添加');
    }


    public function add()
    {
        parent::add(self::$_validate, false);
    }

    public function editUI($id)
    {
        $where['id'] = ':id';
        $notice = $this->baseModel->where($where)->bind($where['id'], $id)->find();
        $notice['content'] = html_entity_decode($notice['content']);
        $this->bcItemPush('编辑公告', U('Notice/editUI/'.$id));
        $this->assign('notice', $notice);
        $this->display('save');
    }

    public function doAddAfter($data, $success){
        $map['create_time'] = time();
        if($success)
        {
            $map['id'] = $success;
            D('Notice')->save($map);
        }
    }

    protected function doEditBefore($data)
    {
        $this->checkName($data, '修改');
    }

    public function edit()
    {
        parent::edit(self::$_validate, false);

    }

    public function doDeleteBefore($id)
    {
    }

    public function delete($id)
    {
        parent::delete($id);
    }

    /**
     * 区域名称不能重复
     * @param $data
     * @param $option
     */
    private function checkName($data, $option)
    {
        $where['title'] = ':title';
        $bind[':title'] = $data['title'];
        $count = $this->baseModel->where($where)->bind($bind)->count();
        if (($option == '修改' && $count >= 2) || ($option == '添加' && $count >= 1)) {
            parent::retJson(false, $option . '失败,公告标题不能重复');
        }
    }



    public function content($id){
        $map['id'] = ':id';
        $bind[':id'] = $id;
        $data = M('Notice')->where($map)->bind($bind)->find();
        $this->bcItemPush('公告内容', U('Notice/content/'.$id));
        $this->assign('content', $data['content']);
        $this->display('detail');


    }
}